package com.douing.ad.dsp.bidding.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Map;

/**
 * @copyright Copyright (c) 2016 - 2017
 * @company ZBJ Network Inc.
 */


/**
 * 验证工具类，提供一系列验证的工具方法
 *
 * @author Yongjun.Wei
 * @since 2016年7月19日
 * @version 1.0.0
 * @description 修改记录：
 *              <table border='1' width='60%'>
 *              <tr>
 *              <td>修改日期</td>
 *              <td>修改人</td>
 *              <td>修改原因</td>
 *              <td>备注</td></th>
 *              <tr>
 *              <td>2016-07-19</td>
 *              <td>Yongjun.Wei</td>
 *              <td>新建</td>
 *              <td>无</td>
 *              </tr>
 *              </table>
 */
public class UtilValidate {

  /**
   * 判断两个对象是否相等. 如果两个对象都为空，返回true。否则通过equals方法对比结果
   *
   * @param obj 对象1
   * @param obj2 对象2
   * @return 对比结果
   */
  public static boolean areEqual(Object obj, Object obj2) {
    if (obj == null) {
      return obj2 == null;
    } else {
      return obj.equals(obj2);
    }
  }

  /** Check whether charsequence c is empty. */
  public static <E> boolean isEmpty(CharSequence charSeq) {
    return (charSeq == null) || charSeq.length() == 0;
  }



  /** Check whether collection c is empty. */
  public static <E> boolean isEmpty(Collection<E> collection) {
    return (collection == null) || collection.isEmpty();
  }


  /** Check whether map m is empty. */
  public static <K, E> boolean isEmpty(Map<K, E> map) {
    return (map == null) || map.isEmpty();
  }

  /**
   * 验证传入的对象是否为空. 针对字符串、集合、Map等数据结构，可以直接用此方法代替
   *
   * @param value Object对象，需要验证的目标对象
   * @return 是否为空true-为空，false-不为空
   */
  @SuppressWarnings("unchecked")
  public static boolean isEmpty(Object value) {
    if (value == null) {
      return true;
    }

    if (value instanceof String) {
      return ((String) value).length() == 0;
    }

    if (value instanceof Collection) {
      return ((Collection<? extends Object>) value).size() == 0;
    }
    if (value instanceof Map) {
      return ((Map<? extends Object, ? extends Object>) value).size() == 0;
    }
    if (value instanceof CharSequence) {
      return ((CharSequence) value).length() == 0;
    }

    // These types would flood the log
    // Number covers: BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
    if (value instanceof Boolean) {
      return false;
    }
    if (value instanceof Number) {
      return false;
    }
    if (value instanceof Character) {
      return false;
    }
    if (value instanceof java.util.Date) {
      return false;
    }
    return false;
  }

  /** Check whether string s is empty. */
  public static boolean isEmpty(String str) {
    return (str == null) || str.length() == 0;
  }

  /** Check whether charsequence c is NOT empty. */
  public static <E> boolean isNotEmpty(CharSequence charSeq) {
    return ((charSeq != null) && (charSeq.length() > 0));
  }

  /** Check whether collection c is NOT empty. */
  public static <E> boolean isNotEmpty(Collection<E> collection) {
    return (collection != null) && !collection.isEmpty();
  }

  /** Check whether an object is NOT empty, will see if it is a String, Map, Collection, etc. */
  public static boolean isNotEmpty(Object obj) {
    return !isEmpty(obj);
  }

  /** Check whether string s is NOT empty. */
  public static boolean isNotEmpty(String str) {
    return (str != null) && str.length() > 0;
  }

  public static boolean isString(Object obj) {
    return ((obj != null) && (obj instanceof String));
  }

  /**
   * 判断一个对象是否所有的值都为null公共方法（被检验的对象不能使用原始数据类型，都使用包装类型）
   *
   * @param obj 要校验的对象
   * @return true 为空，false 不为空
   */
  public static boolean isNullObject(Object obj) {
    if (obj == null) {
      return true;
    }

    Field[] allFields = obj.getClass().getDeclaredFields();
    for (Field field : allFields) {
      // 如果是用private修饰的, 且不是static修饰的
      if ((field.getModifiers() & Modifier.PRIVATE) != 0
          && (field.getModifiers() & Modifier.STATIC) == 0) {
        field.setAccessible(true);
        try {
          if (field.get(obj) != null) {
            return false;
          }
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
      }
    }
    return true;
  }

  public static boolean isNotNullObject(Object obj) {
    return !isNullObject(obj);
  }
}
